<template>
    <app-layout title="订单管理">
        <div class="order-container p-2">
            <el-card class="box-card">
                时间：
                <el-date-picker
                    v-model="times"
                    type="daterange"
                    range-separator="To"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
                />
                场地：
                <el-select v-model="searchData.rooms" multiple></el-select>
                时间段：
                <el-select v-model="searchData.time_slot">
                    <el-option label="午市" value="午市" />
                    <el-option label="晚市" value="晚市" />
                </el-select>
                姓名：
                <el-input
                    v-model="searchData.name"
                    placeholder="请输入姓名"
                ></el-input>
            </el-card>
            <el-table :data="orders" stripe style="width: 100%">
                <el-table-column type="index" label="序号" width="50" />
                <el-table-column label="日期" prop="order_date" width="120" />
                <el-table-column label="场地" prop="name" width="100" >
                    <template #default="scope">
                        <div v-for="(item, idx) in scope.row.rooms" :key="idx">
                            {{ item.name }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="姓名" width="100">
                    <template #default="scope">
                        {{ scope.row.name }}
                    </template>
                </el-table-column>
                <el-table-column label="用餐时间段" prop="time_slot" width="150" />
                <el-table-column label="茶歇" prop="tea" width="100"/>
                <el-table-column label="联系方式" prop="phone" width="150">
                    <template #default="scope">
                        {{ scope.row.customer?.phone }}
                    </template>
                </el-table-column>
                <el-table-column label="预定人数" prop="participants" width="100"/>
                <el-table-column label="餐标" prop="avg_cost" width="100"/>
                <el-table-column label="接单人" prop="book_by" width="100"/>
                <el-table-column label="状态" width="100" >
                    <template #default="scope">
                        <el-tag
                            @click="changeStatus(scope.row)"
                            style="cursor:pointer"
                            :type="
                                scope.row.status === 0
                                    ? 'warning'
                                    : scope.row.status === 1
                                    ? 'success'
                                    : 'danger'
                            "
                            >{{
                                scope.row.status === 0
                                    ? "已预订"
                                    : scope.row.status === 1
                                    ? "已完成"
                                    : "已取消"
                            }}</el-tag
                        >
                    </template>
                </el-table-column>
                <el-table-column label="备注" prop="comment" width="200"/>
                <el-table-column label="总结" prop="conclusion" width="200"/>
                <el-table-column label="操作人" width="200">
                  <template #default="scope">
                    {{scope.row.user?.name}}
                  </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="140">
                    <template #default="scope">
                        <el-button
                            type="primary"
                            :icon="Edit"
                            circle
                            @click="handleOrder(scope.row)"
                        ></el-button>
                        <el-button
                            type="danger"
                            @click="removeOrder(scope.row.id)"
                            :icon="Delete"
                            circle
                        ></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <create-order
                :roomList="groups"
                :dialogFormVisible="roomDialog"
                :orderDate="orderDate"
                @closeDialog="close"
            />
            <el-dialog v-model="statusDialog" title="状态修改">
                <el-radio-group
                    v-model="curStatus.status"
                    size="small"
                    @change="statusChange"
                >
                    <el-radio-button label="0">已预订</el-radio-button>
                    <el-radio-button label="1">已完成</el-radio-button>
                    <el-radio-button label="2">已取消</el-radio-button>
                </el-radio-group>
            </el-dialog>
        </div>
    </app-layout>
</template>

<script>
import CreateOrder from "@/Pages/Booking/CreateOrder.vue";
import { reactive, ref } from "vue";
import AppLayout from "@/Layouts/AppLayout.vue";
import { Inertia } from "@inertiajs/inertia";
import { ElMessage, ElMessageBox } from "element-plus";
import { Search, Edit, Delete } from "@element-plus/icons";
export default {
    name: "Order",
    components: {
        AppLayout,
        CreateOrder,
    },
    props: ["orders", "groups"],
    setup(props) {
        console.log(props.orders);
        let searchData = reactive({
            startTime: "",
            endTime: "",
            rooms: [],
            time_slot: "",
            name: "",
        });
        let curStatus = reactive({
            id: "",
            status: "",
        });
        const changeStatus = (msg) => {
          console.log(1)
            statusDialog.value = true;
            curStatus.status = msg.status;
            curStatus.id = msg.id;
        };
        const times = ref([]);
        const labelWidth = "100px";
        const orderDate = reactive({});
        let form = reactive({
            id: null,
            name: null,
            group_id: null,
            order_type: null,
        });
        let roomDialog = ref(false);
        let statusDialog = ref(false);

        /**
         * 打开房间管理dialog
         */
        const handleOrder = (roomMsg) => {
            for (let key in roomMsg) {
                orderDate[key] = roomMsg[key];
            }
            orderDate.name = orderDate.customer?.name;
            orderDate.phone = orderDate.customer?.phone;
            orderDate.rooms = orderDate.rooms.map((room) => room.room_id);
            roomDialog.value = true;
        };
        const statusChange = (order) => {
            Inertia.post(`/orders/status`, curStatus);
            statusDialog.value = false;
        };
        const close = () => {
            roomDialog.value = false;
        };
        const removeOrder = (id) => {
            ElMessageBox.confirm("确定删除所选房间么？", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    Inertia.delete(`/orders?id=${id}`);
                    ElMessage({
                        type: "success",
                        message: "删除成功",
                    });
                    console.log(id);
                })
                .catch(() => {
                    // catch error
                });
        };

        function getGroup(id) {
            let result = props.groups.filter((group) => group.id === id);
            if (result.length > 0) {
                return result[0].name;
            } else {
                return "";
            }
        }

        return {
            //   tableData,
            Search,
            Edit,
            Delete,
            times,
            form,
            getGroup,
            close,
            labelWidth,
            handleOrder,
            roomDialog,
            removeOrder,
            searchData,
            orderDate,
            statusChange,
            changeStatus,
            statusDialog,
            curStatus
        };
    },
};
</script>

<style scoped>
.box-card .el-input {
    width: 120px !important;
}
</style>
